from rest_framework.generics import ListAPIView, CreateAPIView

from meiduo_admin.user.my_pagination import MyNumberPagination
from users.models import User
from .user_serializers import UserSerializer


class UserView(ListAPIView, CreateAPIView):
    pagination_class = MyNumberPagination
    serializer_class = UserSerializer

    # queryset = User.objects.filter(is_staff=False).all().order_by('date_joined')

    #  重写get_queryset
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return User.objects.filter(is_staff=False, username__contains=keyword).all()
        return User.objects.filter(is_staff=False).all().order_by('date_joined')
